package com.yun.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.reggie.entity.User;
import com.yun.reggie.mapper.UserMapper;
import com.yun.reggie.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @author xiao
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * 根据手机号码查询用户，如果是新用户返回新增的用户
     * 如果是老用户，返回查询到的用户
     * @param phone
     * @return
     */
    @Override
    public User isNewUser(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,phone);
        User user = super.getOne(wrapper);
        if (user == null){
            //如果是新用户，则添加用户并返回
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            super.save(user);
        }
        return user;
    }
}
